package cn.wolfcode.qo;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


import javax.servlet.http.HttpSession;
import java.util.List;

public class UserContext {
    private final static String USER_IN_SESSION = "USER_IN_SESSION";
    private final static String PERMISSIONS_IN_SESSION = "PERMISSIONS_IN_SESSION";

    /*通过Spring Mvc的api获取session对象*/
    public static HttpSession getSession() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attributes.getRequest().getSession();
    }


    public static void setEmployee(Employee employee) {
        getSession().setAttribute(USER_IN_SESSION, employee);
    }


    public static void setPermissions(List<String> permissions) {
        getSession().setAttribute(PERMISSIONS_IN_SESSION, permissions);
    }


    public static Employee getEmployee() {
        Employee employee = (Employee) getSession().getAttribute(USER_IN_SESSION);
        return employee;
    }


    public static List<String> getPermissions() {
        List<String> permissions = (List<String>) getSession().getAttribute(PERMISSIONS_IN_SESSION);
        return permissions;
    }
}
